package com.ten.java.jarupdate;

import com.ten.java.jarupdate.util.IOUtils;
import com.ten.java.jarupdate.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.FileSystemUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

@Slf4j
public class Runner {

    public static void main(String[] args) {
        System.out.println("请按照提示依次输入参数：");

        Scanner scanner = new Scanner(System.in);
        System.out.print("待替换的jar包目录：");
        String unupdateDirPath = scanner.nextLine();

        scanner = new Scanner(System.in);
        System.out.print("jar包中要升级的依赖的名称：");
        String libName = scanner.nextLine();

        scanner = new Scanner(System.in);
        System.out.print("新依赖的路径：");
        String newLibPath = scanner.nextLine();

        scanner = new Scanner(System.in);
        System.out.print("替换后的jar的存放的目录：");
        String updatedDirPath = scanner.nextLine();

        log.info("unupdateDirPath: {}, libName: {}, newLibPath: {}, updatedDirPath: {}", updatedDirPath, libName, newLibPath, updatedDirPath);
        log.info("开始进行升级...");
        try {
            update(unupdateDirPath, libName, newLibPath, updatedDirPath);
            log.info("升级完成。");
        } catch (IOException e) {
            log.error("升级异常：", e);
        }
    }

    public static void update(String unupdateDirPath, String libName, String newLibPath, String updatedDirPath) throws IOException {
        // 创建相关文件
        String updatingDirPath = unupdateDirPath + File.separator + "updating";
        File updatingDir = new File(updatingDirPath);
        File newLib = new File(newLibPath);
        File updatedDir = new File(updatedDirPath);
        updatedDir.mkdirs();

        // 遍历jar包目录下的文件
        File dir = new File(unupdateDirPath);
        File[] jars = dir.listFiles((file, name) -> name.endsWith(".jar"));
        for (File jar : jars) {
            // 复制jar包到临时目录
            updatingDir.mkdirs();
            String jarName = jar.getName();
            File tempJar = new File(updatingDirPath + File.separator + jarName);
            IOUtils.copy(jar, tempJar);

            // 解压该jar包
            String cmd = StringUtils.format("cmd /c c: && cd <> && jar -xvf <>", updatingDirPath, jarName);
            exec(cmd);

            // 删除临时jar包
            tempJar.delete();

            // 遍历lib目录检查目标
            String libPath = updatingDirPath + File.separator + "BOOT-INF\\lib";
            File libDir = new File(libPath);
            File[] files = libDir.listFiles((file, name) -> name.contains(libName));
            if (files != null && files.length == 1) {
                File lib = files[0];
                lib.delete();
                IOUtils.copy(newLib, new File(libPath + File.separator + newLib.getName()));
                cmd = StringUtils.format("cmd /c c: && cd <> && jar -cfM0 <> ./", updatingDirPath, jarName);
                exec(cmd);
                IOUtils.copy(tempJar, new File(updatedDir + File.separator + jarName));
            }
            FileSystemUtils.deleteRecursively(updatingDir);
        }
    }

    private static void exec(String cmd) throws IOException {
        Process process = Runtime.getRuntime().exec(cmd);
        log.info("process inputStream: {}", IOUtils.read(new InputStreamReader(process.getInputStream(), "GBK")));
        log.info("process errorStream: {}", IOUtils.read(new InputStreamReader(process.getErrorStream(), "GBK")));
    }
}
